package com.bruce.phoneFlash;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class FirstActivity extends AppCompatActivity {

    private ViewPager mViewPager;

    private List<View> mViews;
    private ImageView i1;
    private ImageView i2;
    private ImageView i3;
    private ImageView start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        initView();
        initEvent();
    }

    private void initEvent() {

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                startActivity(intent);
                SharedPreferences sp = getSharedPreferences("flash", MODE_PRIVATE);
                SharedPreferences.Editor spe = sp.edit();
                spe.putString("version", "1");
                spe.apply();
                finish();
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setNo();
                switch (position) {
                    case 0:
                        start.setVisibility(View.GONE);
                        i1.setBackgroundResource(R.drawable.pager_checked);
                        break;
                    case 1:
                        start.setVisibility(View.GONE);
                        i2.setBackgroundResource(R.drawable.pager_checked);
                        break;
                    case 2:
                        start.setVisibility(View.VISIBLE);
                        i3.setBackgroundResource(R.drawable.pager_checked);
                        break;
                }
            }

            private void setNo() {
                i1.setBackgroundResource(R.drawable.pager_no);
                i2.setBackgroundResource(R.drawable.pager_no);
                i3.setBackgroundResource(R.drawable.pager_no);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        start = (ImageView) findViewById(R.id.start);
        i1 = (ImageView) findViewById(R.id.i_1);
        i2 = (ImageView) findViewById(R.id.i_2);
        i3 = (ImageView) findViewById(R.id.i_3);

        View view1 = getLayoutInflater().inflate(R.layout.layout1, null);
        View view2 = getLayoutInflater().inflate(R.layout.two, null);
        View view3 = getLayoutInflater().inflate(R.layout.three, null);
        mViews = new ArrayList<>();
        mViews.add(view1);
        mViews.add(view2);
        mViews.add(view3);
        PagerAdapter pager = new Pager();
        mViewPager.setAdapter(pager);
    }

    private class Pager extends PagerAdapter {

        @Override
        public int getCount() {
            return mViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViews.get(position));

            return mViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViews.get(position));
        }
    }
}
